package edu.co.gap.util;

import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;


import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_legacy.*;
import static com.googlecode.javacv.cpp.opencv_features2d.*;
import static com.googlecode.javacv.cpp.avutil.*;
import static com.googlecode.javacv.cpp.avformat.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Savepoint;

import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_highgui.CV_LOAD_IMAGE_GRAYSCALE;
public class FileHelper {

	public static boolean fileExist(String filePath) {
		
		   File imageFile = new File(filePath);
		   
		  return imageFile.exists();
	}
	
	public static void writeFile(String filepath,byte[] bytes) throws IOException {
		OutputStream out = new FileOutputStream(filepath);
		out.write(bytes);
		out.close();
	}
	
	public static byte[] LoadImageInBytes(String filePath) throws Exception
	{
	    File file = new File(filePath);

	    int size = (int)file.length();

	    byte[] buffer = new byte[size];

	    FileInputStream in = new FileInputStream(file);

	    in.read(buffer);        

	    in.close();

	    return buffer;
	}

	public static IplImage loadIplImage(String imageFilepath,int CV_MODE,int Height,int Width) throws IOException {
		
		IplImage iplImage=cvLoadImage(imageFilepath, CV_MODE);
		IplImage destino=cvLoadImage("C:/programacion/java/workspace/android/Servidor GAP/images/s1/1.pgm", CV_MODE);
//		IplImage destino=cvLoadImage(imageFilepath, CV_MODE);
//		IplImage iplImage=cvLoadImage("C:/programacion/java/workspace/android/Servidor GAP/images/s2/1.pgm", CV_MODE);
				                if (iplImage.height() > destino.height() || iplImage.width() > destino.width())
				                {
				                        cvResize(iplImage, destino, CV_INTER_AREA);
				                }
				                else
				                {
				                        cvResize(iplImage, destino, CV_INTER_LINEAR);
				                } 
//		cvEqualizeHist(iplImage, iplImage); 
		
		cvSaveImage("converted.pgm", destino);
		
		return destino;
	}
  	public static IplImage loadIplImage(String imageFilepath,int CV_MODE) throws IOException {
		
		IplImage iplImage=cvLoadImage(imageFilepath, CV_MODE);
		return iplImage;
	}
	public static void main(String[] args) {
		try {
			loadIplImage("images/s1/input.jpg", CV_LOAD_IMAGE_GRAYSCALE, 92, 112);
			
//			IplImage a=loadIplImage("C:/Users/David/Documents/IMAGES/input.jpg", CV_LOAD_IMAGE_GRAYSCALE);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
